package org.peterf.foxish.component.updater;

import org.peterf.foxish.component.property.Updatable;
import org.peterf.foxish.component.updater.interfaces.Gradual;

public class VibrateUpdater implements Gradual {

    private static final float OFFSET = 0.06f;

    private static final float[][] STAGES = new float[][] {
            { 1.0f, 1.0f - OFFSET, 1.0f, 1.0f + OFFSET, 8f },
            { 1.0f - OFFSET, 1.0f + OFFSET, 1.0f + OFFSET, 1.0f - OFFSET, 4f },
            { 1.0f + OFFSET, 1.0f, 1.0f - OFFSET, 1.0f, 8f } };

    private boolean enabled = true;

    private int loop;

    private ScaleUpdater scaleUpdater = new ScaleUpdater(0.0f, 0.0f, 0.0f, 0.0f);

    private int stage = -1;

    private float stageProgress = 1.0f;

    public VibrateUpdater(int loop) {
        this.loop = loop;
        // TODO Wow, loop is ignored.
    }

    public float getProgress() {
        // TODO Do we need this?
        return 0.0f;
    }

    public boolean isFinished() {
        return stage >= STAGES.length;
    }

    public void reset() {
        stage = -1;
        stageProgress = 1.0f;
        enabled = true;
    }

    public VibrateUpdater setEnabled(boolean enabled) {
        this.enabled = enabled;
        return this;
    }

    public void setProgress(float progress) {
        // TODO Do we need this?
    }

    public void update(Updatable updatable, float elapsed) {
        if (enabled) {
            if (stageProgress >= 1.0f) {
                if (++stage >= STAGES.length) {
                    return;
                }
                stageProgress = 0.0f;
                scaleUpdater.reset(STAGES[stage][0], STAGES[stage][1],
                        STAGES[stage][2], STAGES[stage][3]);
            }
            stageProgress += elapsed * STAGES[stage][4];
            scaleUpdater.setProgress(stageProgress);
            scaleUpdater.update(updatable, elapsed);
        }
    }

}
